<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var name = '栓';
        var quan = {
            name:'铨'
        };
        function sayName(str){
            console.log(this.name)
            console.log(str)
        }
        Function.prototype.myCall = function(context){
            if(typeof this !== 'function'){
                throw new TypeError('not function')
            }
            context = context || window
            context.fn = this
            console.log(arguments)
            let args = [...arguments].slice(1)
            // 如果传来参数，这个就是传过来的参数
            console.log(args)    //['tt']
            let result = context.fn(...args)
            delete context.fn
            return result;
        }
        // 简化方法--2022-12-03
        Function.prototype.myCall2 = function(context,...args){
            if(typeof this !== 'function'){
                throw new TypeError('not function')
            }
            console.log(this)
            console.log(context)
            console.log(args)
            context.fn = this
            let result = context.fn(...args)
            delete context.fn
            return result;
        }
        sayName()
        sayName.myCall(quan,'tt')
        sayName.myCall2(quan,'tt','tt2')
    </script>
</body>
</html>